


async function checkStatus(key) {
    const res = await axios({//查询二维码状态
        url: `/login/qr/check?key=${key}&timerstamp=${Date.now()}`,
        withCredentials: true, //关键
    })
    return res.data
}
async function getLoginStatus() {
    const res = await axios({
        url: `/login/status?timerstamp=${Date.now()}`,
        withCredentials: true, //关键
    })
    return res;

}
let timer //定义全局变量便于关闭定时器
async function qrLogin() {
    // $('.qr').hide()
    quit()
    $('.qr-login').show()
    $('#qrImg').removeClass('covered')
    this.getLoginStatus()

    const res = await axios({
        url: `/login/qr/key?timerstamp=${Date.now()}`,
        withCredentials: true, //关键
    })
    const key = res.data.data.unikey
    const res2 = await axios({//请求二维码
        url: `/login/qr/create?key=${key}&qrimg=true&timerstamp=${Date.now()}`,
        withCredentials: true, //关键
    })
    // console.log(res2.data.data.qrimg);
    $("#qrImg").attr({ src: `${res2.data.data.qrimg}` })

    timer = setInterval(async () => {//重复查询二维码状态，并进行操作
        const statusRes = await this.checkStatus(key)
        if (statusRes.code === 800) {//当二维码过期时的操作
            $('.qr').show()
            $('#qrImg').addClass('covered')
            clearInterval(timer)
        }
        if (statusRes.code === 803) {//二维码校验成功，对返回的用户信息进行处理
            // 这一步会返回cookie
            clearInterval(timer)
            const res = await this.getLoginStatus()
            alert('授权登录成功')
            document.querySelector('#limg').src = res.data.data.profile.avatarUrl
            $('#login').hide()
            $('#limg').show()
            $('.qr-login').hide()
            $('.log').hide()

        }
    }, 3000)
}

async function pwdLogin(phone, password) {
    const res = await axios({
        url: `/login/cellphone`,
        method: 'post',
        data: {
            phone: phone,
            password: password,
        },
    })
    return res
}
async function phoneLogin() {
    const phone = $('.tel').val()
    const password = $('.pwd').val()
    const res = await pwdLogin(phone, password)
    let res2 = await axios({
        url: `/login/status`,
        method: 'post',
        data: {
            cookie: res.data.cookie,
        },
    })
    if (res.data.code === 502) {
        alert('密码错误')
    }
    if(res.data.code==400){
        alert('账号错误')
    }
    if (res.data.code === 200) {
        document.querySelector('#limg').src = res.data.profile.avatarUrl
        $('#login').hide()
        $('#limg').show()
        $('.qr-login').hide()
        $('.log').hide()
    }
}

function close1() {
    clearInterval(timer);
    $('.qr-login').hide()
}
function switch1() {
    $('.qr-plate').toggle();
    $('.phone-plate').toggle();
}
function phone() {
    if (!($('#clause').prop('checked'))) {
        alert('请先勾选同意《服务条款》、《隐私条款政策》、《儿童隐私政策》')
        return
    }
    $('.phone-plate').hide();
    $('.password-plate').show();
}
function back() {
    $('.password-plate').hide();
    $('.phone-plate').show();

}

function quit() {
    axios({
        url: `/logout`,
        method: `post`,
    })
    $('.log').show()
}